<template>
	<view class="page">
		<uni-forms :modelValue="versionInfo" ref="form" :rules="rules">
			<uni-forms-item label="软件名称">
				<view class="name">{{versionInfo.name}}</view>
			</uni-forms-item>
			<uni-forms-item label="版本号" name="versionCode">
				<uni-easyinput type="text" v-model="versionInfo.versionCode" placeholder="请输入版本号" />
			</uni-forms-item>
			<uni-forms-item label="更新地址" name="updateUrl">
				<uni-easyinput type="text" v-model="versionInfo.updateUrl" placeholder="请输入更新地址" />
			</uni-forms-item>
		</uni-forms>
		<view class="desc-block"
		v-for="i in 10"
		:key = "i"
		v-if="descIndex >= i">
			<uni-easyinput 
			type="text" 
			v-model="descriptionList[i-1]" 
			placeholder="请输入更新日志" />
			<label class="iconfont iconjiahao" v-if="i-1 == 0" @click="addDesc()"></label>
			<label class="iconfont iconziyuan1" v-else @click="rmDesc()"></label>
		</view>
		<button @click="submitForm" type="primary">发布新版本</button>
	</view>
</template>

<script>
	
	var _self
	
	export default {
		data() {
			return {
				formData:{
					name:'',
					age:'',
					hobby:[]
				},
				rules: {
					versionCode: {
						rules: [
							{
								required: true,
								errorMessage: '请输入版本号',
							}
						]
					},
					updateUrl: {
						rules: [{
							required: true,
							errorMessage: '请输入更新地址',
						}]
					}
				},
				versionInfo: {
					name: "扫雷联机版",
					versionCode: "", // 版本号
					updateUrl: "", // 更新地址
					description: "", // 描述
				},
				descriptionList: [],
				descIndex: 1
			}
		},
		onLoad() {
			_self = this
		},
		methods: {
			addDesc(){
				this.descIndex ++
				if(this.descIndex > 10){
					uni.showToast({
						icon:"none",
						title:"最多只能有十条更新日志"
					})
					this.descIndex = 10
				}
			},
			rmDesc(){
				if(this.descIndex ==  1){
					return
				}
				this.descIndex --
			},
			submitForm(form) {
				if(
				_self.versionInfo.versionCode.trim() == ""||
				_self.versionInfo.updateUrl.trim() == ""||
				_self.descriptionList.length == 0
				){
					uni.showToast({
						icon:"none",
						title:"请填写齐全信息"
					})
				}else{
					// 将数组转换为字符串
					this.versionInfo.description = this.descriptionList.join("*")
					uni.showLoading({
						mask:true,
						title:"发布新版本中..."
					})
					uni.request({
						url: _self.utils.basicUrl+"/saoleiVersion",
						method: "POST",
						data: {
							..._self.versionInfo
						},
						success: function(res) {
							if(res.data.code === 20000){
								uni.showToast({
									icon:"none",
									title:"发布新版本成功"
								})
								setTimeout(function(){
									uni.navigateBack({
										delta:1
									})
								},500)
							}else{
								_self.utils.requestFail(res)
								// uni.showToast({
								// 	icon:"none",
								// 	title: res.data,
								// 	position:"center"
								// })
							}
						},
						fail: () => {
							uni.showToast({
								icon:"none",
								title: '网络错误(⊙ˍ⊙)',
								position:"center"
							})
						},
						complete() {
							uni.hideLoading()
						}
					})
				}
				
			}
		}
	}
</script>

<style>
	.page{
		padding: 30rpx;
	}
	.desc-block{
		display: flex;
		flex-direction: row;
		margin-bottom: 20rpx;
	}
	.iconjiahao{
		margin-left: 20rpx;
		font-size: 40rpx;
		line-height: 70rpx;
		font-weight: bold;
	}
	.iconziyuan1{
		margin-left: 20rpx;
		font-size: 40rpx;
		line-height: 70rpx;
		font-weight: bold;
		color: #ff0000;
	}
	.name{
		line-height: 70rpx;
	}
</style>
